package com.kinglone.back.file.controller;

import com.kinglone.back.file.model.BasFile;
import com.kinglone.back.file.service.FileService;
import com.kinglone.back.pdf.model.Pdfdto;
import com.kinglone.back.pdf.service.PdfService;
import com.kinglone.common.entity.PageBean;
import com.kinglone.common.utils.ResponseUtil;
import com.kinglone.common.utils.ResultMsg;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/uploadfile")
public class BasFileController {

    protected final static Logger logger = LogManager.getLogger(BasFileController.class);
    @Autowired
    private FileService fileService;
    @Autowired
    private PdfService pdfService;

    @RequestMapping("/showFile")
    public String showFile(HttpServletRequest request){
        System.out.println("---------------------------");
        logger.info("跳转到文件列表");
        return "file/fileList";
    }
    @RequestMapping("/fileList")
    public String fileList(String page, String limit, BasFile basFile, HttpServletResponse response){
        logger.info("----------------------查询文件列表信息");
        try {
            PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(limit));
            JSONObject result = new JSONObject();

            Map<String, Object> map = new HashMap<String, Object>();
            // map.put("param",role);
            map.put("start", pageBean.getStart());
            map.put("size", pageBean.getPageSize());

            List<BasFile> rolelist = fileService.list(map);
            int count = fileService.getTotal(map);
            JSONArray jsonArray = JSONArray.fromObject(rolelist);
            result.put("code",0);
            result.put("msg","");
            result.put("data", jsonArray);
            result.put("count", count);
            System.out.println(result);
            ResponseUtil.write(response, result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

   /* @RequestMapping("/createPdf")
    @ResponseBody
    public ResultMsg createPdf(Pdfdto pdfdto) {
        ResultMsg msg = new ResultMsg();
        try {
            msg = pdfService.createpDF(pdfdto);
        }catch (Exception e){
            e.printStackTrace();
            msg.setSuccess(false);
            msg.setMessage("pdf生成失败!");
            return msg;
        }
        return msg;
    }*/
}
